/*
 * @Author: ziqi jhzq12345678
 * @Date: 2024-10-09 09:23:30
 * @LastEditors: ziqi jhzq12345678
 * @LastEditTime: 2025-01-21 09:57:52
 * @FilePath: /pornhub_app/lib/src/views/search/result/search_comics_result.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import 'package:pornhub_app/components/keep_alive_wrapper.dart';
import 'package:pornhub_app/components/no_more/no_data.dart';
import 'package:pornhub_app/components/no_more/no_more.dart';

import 'package:pornhub_app/http/http_service.dart';

import 'package:pornhub_app/model/search/search_res_model.dart';
import 'package:pornhub_app/model/search/search_user_model.dart';
import 'package:pornhub_app/views/search/result/search_user_cell.dart';

import 'package:pornhub_app/views/search/search_controller.dart';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';

class SearchUserResult extends StatefulWidget {
  final int searchType;
  const SearchUserResult({super.key, required this.searchType});

  @override
  State<SearchUserResult> createState() => _SearchComicsResult();
}

class _SearchComicsResult extends State<SearchUserResult> {
  final _pageSize = 30;

  final PagingController<int, SearchUserModel> _pagingController =
      PagingController(firstPageKey: 1);
  final searchvc = Get.find<SearchPageController>();
  Future _initListData(int pageKey) async {
    final resp = await httpInstance.get<SearchResModel>(
        url: 'search/keyWord',
        queryMap: {
          'pageSize': _pageSize,
          'searchType': widget.searchType,
          'searchWord': searchvc.keyWord,
          'page': pageKey,
        },
        complete: SearchResModel.fromJson);

    final isLastPage = resp.userList.length < _pageSize;
    if (isLastPage) {
      _pagingController.appendLastPage(resp.userList);
    } else {
      final nextPageKey = pageKey + 1;
      _pagingController.appendPage(resp.userList, nextPageKey as int?);
    }
  }

  @override
  void initState() {
    _pagingController.addPageRequestListener((pageKey) {
      _initListData(pageKey);
    });
    ever(searchvc.keyWord, (v) {
      _pagingController.refresh();
    });
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return KeepAliveWrapper(
      child: RefreshIndicator(
        onRefresh: () => Future.sync(
          () => _pagingController.refresh(),
        ),
        child: PagedListView(
          padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 8.w),
          pagingController: _pagingController,
          builderDelegate: PagedChildBuilderDelegate<SearchUserModel>(
              noMoreItemsIndicatorBuilder: (_) {
            return const NoMore();
          }, noItemsFoundIndicatorBuilder: (_) {
            return const NoData();
          }, itemBuilder: (context, value, index) {
            return SearchUserCell(
              model: value,
            ).marginOnly(bottom: 15.w);
          }),
        ),
      ),
    );
  }
}
